import axios from 'axios'

const instance = axios.create({
  baseURL: 'https://localhost:8123/api/', // Replace with your API base URL
  timeout: 5000, // Request timeout in milliseconds
  headers: {
    'Content-Type': 'application/json',
  },
})

// Request interceptor
instance.interceptors.request.use(
  (config) => {
    // Add authorization token or other custom logic before request is sent
    const token = localStorage.getItem('authToken')
    if (token) {
      config.headers.Authorization = token
    }
    return config
  },
  (error) => {
    // Handle request error
    return Promise.reject(error)
  },
)

// Response interceptor
instance.interceptors.response.use(
  (response) => {
    // Process the response data
    return response
  },
  (error) => {
    // Handle response errors
    if (error.response) {
      // Server responded with a status other than 2xx
      console.error('Response error:', error.response.status, error.response.data)
    } else if (error.request) {
      // Request was made but no response received
      console.error('No response received:', error.request)
    } else {
      // Something else caused the error
      console.error('Error:', error.message)
    }
    return Promise.reject(error)
  },
)

export default instance
